package com.viettel.rmsmobile;

import java.util.ArrayList;
import java.util.List;

import com.viettel.BO.RecruitmentAnouncementBean;
import com.viettel.menu.PlanetFragment;
import com.viettel.rmsmobile.R;
import com.viettel.synctask.RecruitmentAnouncementSyncTask;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import com.viettel.data.RecruitmentAnouncementFetcher;

public class FindJob extends Fragment {

	public static final String ARG_POSITION = "position_menu";
	EditText txtKeySearch;
	Spinner cbLocation;
	Spinner cbPosition;
	TextView txtView;
	EditText txtText;
	private ListFindJobAdapter adapter;
	private ListView listFindJob;
	private List<RecruitmentAnouncementBean> jobList = new ArrayList();
	RecruitmentAnouncementFetcher recruitmentAnouncementFetcher = new RecruitmentAnouncementFetcher(
			null, null);

	public FindJob() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View findJobView = inflater
				.inflate(R.layout.find_job, container, false);
		int i = getArguments().getInt(ARG_POSITION);

		// Lay cac doi tuong
		cbPosition = (Spinner) findJobView.findViewById(R.id.cbPosition);
		cbLocation = (Spinner) findJobView.findViewById(R.id.cbLocation);
		listFindJob = (ListView) findJobView.findViewById(R.id.listJob);

		// Gan data source vao Adapter
		// Context context;
		// ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(
		// getActivity(), android.R.layout.select_dialog_item,
		// locationList);
		// ArrayAdapter<String> positionAdapter = new ArrayAdapter<String>(
		// getActivity(), android.R.layout.select_dialog_item,
		// positionList);
		//
		// // Gan danh sach cho Spinner
		// locationAdapter
		// .setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		// cbLocation.setAdapter(locationAdapter);
		// cbLocation.setOnItemSelectedListener(new LocationProcessEvent());
		//
		// positionAdapter
		// .setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		// cbPosition.setAdapter(positionAdapter);
		// cbPosition.setOnItemSelectedListener(new PositionProcessEvent());

		RecruitmentAnouncementSyncTask anouncementSyncTask = new RecruitmentAnouncementSyncTask();
		anouncementSyncTask.execute(this);
		return findJobView;
	}

	/**
	 * Hien thi danh sach cong viec
	 */
	private void setView() {
		if (jobList != null && !jobList.isEmpty()) {
			adapter = new ListFindJobAdapter(getActivity(),
					R.layout.item_find_job, jobList);
			// View List
			listFindJob.setAdapter(adapter);
			listFindJob.setOnItemClickListener(new FindJobClickListener());
		}
	}

	/**
	 * Thiet lap danh sach
	 * 
	 * @param response
	 */
	public void setJoblist(List<RecruitmentAnouncementBean> response) {
		jobList = response;
		setView();
	}

	private class FindJobClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectJob(position);
		}
	}

	private void selectJob(int position) {
		Fragment fragment=new ViewJobDetailActivity();
		Bundle args = new Bundle();
		RecruitmentAnouncementBean item = jobList.get(position);
		args.putLong("recruitmentAnouncementId", item.getRecruitmentAnouncementId());
		args.putString("positionName", item.getPositionName());
		args.putString("organizationName", item.getOrganizationName());
		args.putString("locationName", item.getLocationName());
		args.putString("jobDescription", item.getJobDescription());
		args.putString("jobRequire", item.getJobRequire());
		args.putString("globalRequire", item.getGlobalRequire());
		args.putString("endDate", item.getEndDate());
		args.putLong("quantity", item.getQuantity());
		args.putString("otherRequire", item.getOtherRequire());
		args.putString("address", item.getAddress());
		args.putString("recruitmentName", item.getRecruitmentName());
		args.putString("type", item.getType());
		args.putString("interest", item.getInterest());
		fragment.setArguments(args);
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.content_frame, fragment).commit();
	}

	// Class tao su kien
	private class LocationProcessEvent implements OnItemSelectedListener {
		// Khi chon item
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// selection.setText(arr[arg2]);
			//txtText.setText(locationList[arg2]);
		}

		// Neu khong co tac dong
		public void onNothingSelected(AdapterView<?> arg0) {
			// selection.setText("");
		}
	}

	// Class tao su kien
	private class PositionProcessEvent implements OnItemSelectedListener {
		// Khi chon item
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			//txtText.setText(positionList[arg2]);
		}

		// Neu khong co tac dong
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
}
